#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/3/15 14:36
# @Author  : 王凯
# @File    : __init__.py.py
# @Project : scrapy_spider
import os
import subprocess
import threading
from pathlib import Path

from utils.logs import logger

lock = threading.Lock()


def git_add_file(file_path):
    lock.acquire()
    try:
        logger.info(f"正在提交文件 {file_path}")
        subprocess.run(f"git add {file_path}".split(), check=True)
    finally:
        # 释放锁
        lock.release()


def run(name, file_path):
    subprocess.run(f"scrapy crawl {name}".split(), check=True)

    git_add_file(file_path)


if __name__ == "__main__":
    # 取该文件夹下的所有文件名称
    file_list = os.listdir(os.path.dirname(os.path.abspath(__file__)))
    for file_name in file_list:
        if file_name.endswith(".py") and file_name != "__init__.py":
            # 将文件名转换成模块名
            module_name = file_name[:-3]
            file_path = Path(__file__).parent / file_name
            run(module_name, file_path)
